package org.dfl.plugin.editor.actions;

import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.model.DflDiagram;
import org.dfl.plugin.utils.WorkbenchUtil;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.WorkbenchException;


/**
 * stop flow running action
 * @author Piotr Wlodarczyk
 *
 */
public class StopAction extends org.eclipse.gef.ui.actions.SelectionAction {
	public static final String ACTION_ID = "StopDfl"; //$NON-NLS-1$
	private DflDiagram diagram;

	public StopAction(IWorkbenchPart part, DflDiagram diagram) {
		super(part);
		this.diagram = diagram;
		setId(ACTION_ID);
		setImageDescriptor(DflPlugin.getImageDescriptor("icons/eclipse/stop.gif")); //$NON-NLS-1$
		setHoverImageDescriptor(getImageDescriptor());
		setText(Messages.StopAction_0);
		setToolTipText(Messages.StopAction_0);
	}

	protected boolean calculateEnabled() {
		return (diagram!=null && diagram.isRunnning());
	}
	
	public void run() {
		diagram.stop();
		diagram.clearData();
		diagram.setEditable(true);
		
		// change the perspective
		try {
			WorkbenchUtil.getActiveWindow().getWorkbench().showPerspective("org.dfl.plugin.perspectives.dflperspective", WorkbenchUtil.getActiveWindow()); //$NON-NLS-1$
		} catch (WorkbenchException e) {}
		
	}
}
